home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DESDOS.ARJ / GETOPT.C < prev    next >
C/C++ Source or Header  |  1990-08-29  |  2KB  |  62 lines

  1. /* got this off net.sources */
  2. #include <stdio.h>
  3.  
  4. #ifdef MSDOS
  5. #include <string.h>
  6. #endif
  7.  
  8. /*
  9.  * get option letter from argument vector
  10.  */
  11. int    opterr = 1,        /* useless, never set or used */
  12.     optind = 1,        /* index into parent argv vector */
  13.     optopt;            /* character checked for validity */
  14. char    *optarg;        /* argument associated with option */
  15.  
  16. #define BADCH    (int)'?'
  17. #define EMSG    ""
  18. #define tell(s)    fputs(*nargv,stderr);fputs(s,stderr); \
  19.         fputc(optopt,stderr);fputc('\n',stderr);return(BADCH);
  20.  
  21. getopt(nargc,nargv,ostr)
  22. int    nargc;
  23. char    **nargv,
  24.     *ostr;
  25. {
  26.     static char    *place = EMSG;    /* option letter processing */
  27.     register char    *oli;        /* option letter list index */
  28.  
  29. #ifndef MSDOS
  30.     char    *index();
  31. #else
  32. #define index(s,c) strchr(s,c)
  33. #endif
  34.  
  35.     if(!*place) {            /* update scanning pointer */
  36.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  37.         if (*place == '-') {    /* found "--" */
  38.             ++optind;
  39.             return(EOF);
  40.         }
  41.     }                /* option letter okay? */
  42.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  43.         if(!*place) ++optind;
  44.         tell(": illegal option -- ");
  45.     }
  46.     if (*++oli != ':') {        /* don't need argument */
  47.         optarg = NULL;
  48.         if (!*place) ++optind;
  49.     }
  50.     else {                /* need an argument */
  51.         if (*place) optarg = place;    /* no white space */
  52.         else if (nargc <= ++optind) {    /* no arg */
  53.             place = EMSG;
  54.             tell(": option requires an argument -- ");
  55.         }
  56.          else optarg = nargv[optind];    /* white space */
  57.         place = EMSG;
  58.         ++optind;
  59.     }
  60.     return(optopt);            /* dump back option letter */
  61. }
  62.